# Create your models here.
from django.db import models
from user.models import SysUser


class GroupList(models.Model):
    """服务器分组表"""
    name = models.CharField(max_length=45, verbose_name="组名", unique=True)
    parent_id = models.IntegerField(null=True, verbose_name="父分组ID")
    layered_id = models.SmallIntegerField(null=True, verbose_name="分级ID", default=0)
    create_time = models.DateTimeField(null=True, verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(null=True, verbose_name="更新时间", auto_now=True)

    class Meta:
        verbose_name = "服务器分组表"
        db_table = "hosts_mgr_group_list"


class HostList(models.Model):
    """服务器主机表"""
    hostname = models.CharField(unique=True, max_length=45, verbose_name="主机名称")
    public_ip_addr = models.GenericIPAddressField(null=True, verbose_name="公网IP地址")
    private_ip_addr = models.GenericIPAddressField(null=True, verbose_name="内网IP地址")
    connect_ip = models.GenericIPAddressField(verbose_name="连接IP地址")
    config_info = models.TextField(null=True, verbose_name="配置信息")
    server_type = models.SmallIntegerField(verbose_name="主机类型：1、linux,2、windows")
    password = models.TextField(verbose_name="密码")
    port = models.SmallIntegerField(verbose_name="SSH端口")
    rdp_port = models.SmallIntegerField(null=True, verbose_name="RDP端口")
    user = models.CharField(max_length=45, verbose_name="用户名")
    status = models.SmallIntegerField(null=True, verbose_name="状态(0验证失败,1未验证,2已验证)")
    create_time = models.DateTimeField(null=True, verbose_name="创建时间", auto_now_add=True)
    update_time = models.DateTimeField(null=True, verbose_name="更新时间", auto_now=True)
    remark = models.TextField(null=True, verbose_name="备注")
    sort_num = models.IntegerField(null=True, verbose_name="显示顺序")

    class Meta:
        verbose_name = "服务器主机表"
        db_table = "hosts_mgr_host_list"


class GroupHost(models.Model):
    """服务器主机和分组关联表"""
    host = models.ForeignKey(HostList, on_delete=models.PROTECT, verbose_name="关联主机")
    group = models.ForeignKey(GroupList, on_delete=models.PROTECT, verbose_name="关联主机分组")

    class Meta:
        verbose_name = "服务器主机和分组关联表"
        db_table = "hosts_mgr_group_host"


class CmdRecord(models.Model):
    """命令批量执行记录表"""
    cmd_content = models.TextField(verbose_name="命令内容")
    hostname = models.TextField(verbose_name="主机名称")
    status = models.SmallIntegerField(verbose_name="执行状态：1：成功、0：失败")
    user = models.CharField(max_length=45, verbose_name="执行用户")
    execution_time = models.DateTimeField(verbose_name="执行时间", auto_now_add=True)

    class Meta:
        verbose_name = "命令批量执行记录表"
        db_table = "hosts_mgr_cmd_record"


class FileDistribution(models.Model):
    """文件分发记录表"""
    path = models.TextField(verbose_name="分发路径")
    hostname = models.CharField(max_length=45, verbose_name="主机名称")
    user = models.CharField(max_length=45, verbose_name="分发用户")
    status = models.SmallIntegerField(verbose_name="分发状态：1：成功、2：失败")
    distribution_time = models.DateTimeField(null=True, verbose_name="分发时间", auto_now_add=True)

    class Meta:
        verbose_name = "文件分发记录表"
        db_table = "hosts_mgr_file_distribution"
